package com.cgx.tablet.beeper.base;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cgx.tablet.beeper.R;


/**
 * Fragment基类
 */
public abstract class BaseFragment extends BaseLazyLoadFragment {

    private View rootView;
    private Dialog loadingDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getContentViewId(), container, false);
            bind(rootView);
            initAction();
            isViewPrepare = true;//懒加载的标记，视图是否已加载
            lazyLoad();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    /**
     * 动作初始化
     */
    protected void initAction() {
    }


    /**
     * 获取布局id抽象方法
     *
     * @return 布局文件资源id
     */
    protected abstract int getContentViewId();

    /**
     * 为dataBinding服务
     *
     * @param view 布局文件对象
     */
    protected abstract void bind(View view);


    /**
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls, String flag, String msg) {
        Intent intent = new Intent();
        intent.setClass(getContext(), cls);
        if (!TextUtils.isEmpty(msg) && !TextUtils.isEmpty(flag)) {
            intent.putExtra(flag, msg);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls, String flag, long id) {
        Intent intent = new Intent();
        intent.setClass(getContext(), cls);
        intent.putExtra(flag, id);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls, String flag, int id) {
        Intent intent = new Intent();
        intent.setClass(getContext(), cls);
        intent.putExtra(flag, id);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getContext(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * 通过Action跳转界面
     **/
    protected void startActivity(Intent intent, String action) {
        startActivity(action, null);
    }

    /**
     * 含有Bundle通过Action跳转界面
     **/
    protected void startActivity(String action, Bundle bundle) {
        Intent intent = new Intent();
        intent.setAction(action);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * 显示加载对话框
     **/
    protected void showLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new Dialog(getContext(), R.style.Translucent_NoTitle);
            loadingDialog.setContentView(R.layout.dialog_loading);
            loadingDialog.setCanceledOnTouchOutside(false);
        }
        if (!loadingDialog.isShowing())
            loadingDialog.show();
    }

    /**
     * 隐藏加载对话框
     **/
    protected void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
    }

}
